#include "widget.h"
#include "ui_widget.h"
#include "utils.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent)
    , m_pixmap(QPixmap(":/res/writing.png"))
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setGeometry(0, 0, qApp->desktop()->width(), qApp->desktop()->height());
//    this->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnBottomHint | Qt::Tool);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus | Qt::WindowStaysOnTopHint | Qt::Tool);
    this->setAttribute(Qt::WA_TranslucentBackground, true);//设置背景透明
    this->setWindowState(Qt::WindowNoState | Qt::WindowFullScreen);
    this->setFocusPolicy(Qt::NoFocus);
    this->setWindowOpacity(0.6);

    Utils::passInputEvent(this->winId());
}

Widget::~Widget()
{
    delete ui;
}

void drawRotatedText(QPainter *painter, float degrees, int x, int y, const QString &text)
{
    painter->save(); //保存原来坐标系统
    painter->translate(x, y); //平移坐标原点到 x， y
    painter->rotate(degrees); //坐标旋转degrees 度
    painter->drawText(0, 0, text); //在原点绘制文本
    painter->restore(); //回复原来的坐标系统
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    qreal ratio = qApp->devicePixelRatio();
    QPoint iconPoint = QPoint(this->width() / ratio - m_pixmap.width(), this->height() / ratio - m_pixmap.height());//右下角
    painter.setOpacity(1);
    painter.drawPixmap(iconPoint, m_pixmap);

    QString text = "=>屏幕水印信息<=";

    //QRectF textRect(iconPoint.x(), iconPoint.y(), m_pixmap.width(), m_pixmap.height());
    QFont font = painter.font();
    font.setPointSize(15);
    painter.setFont(font);
    painter.setOpacity(0.5);
    painter.setPen(QPen(QColor("#0f0f0f")));
    //painter.drawText(textRect, Qt::AlignCenter, text);

    for (int i=0; i<this->width(); i=i+m_pixmap.width()){
        for (int j=0; j<this->height(); j=j+m_pixmap.height()){
            QRectF textRect(i, j, m_pixmap.height(), m_pixmap.width());
            //painter.drawText(textRect, Qt::AlignCenter, text);
            drawRotatedText(&painter, 45, i, j+10, text);
            drawRotatedText(&painter, 45, i, j+40, "www.wondersoft.cn");
            drawRotatedText(&painter, 45, i, j+70, "192.168.100.100");
        }
    }
}
